package com.akk.module.avs.service;

import com.akk.module.avs.entity.AvsPartner;
import com.akk.module.avs.entity.AvsPartnerExample;
import com.akk.module.avs.mapper.AvsPartnerMapper;
import com.akk.utils.FileUtil;
import com.akk.utils.JExcelService;
import com.akk.utils.ListUtils;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class AvsPartnerService {
    private static Logger logger = LoggerFactory.getLogger(AvsPartnerService.class);

    @Autowired
    private AvsPartnerExample partnerExample;

    @Autowired
    private AvsPartnerMapper partnerMapper;

    @Autowired
    private JExcelService jExcelService;

    public List<AvsPartner> queryList(AvsPartner partner) {
        if (partner.getTyped() == 0) {
            partnerExample.clear();
            partnerExample.createCriteria().andIdCardEqualTo(partner.getIdCard()).andTypedEqualTo(partner.getTyped());
            return partnerMapper.selectByExample(partnerExample);
        } else {
            partnerExample.clear();
            partnerExample.createCriteria().andPartnerNameEqualTo(partner.getPartnerName()).andTypedEqualTo(partner.getTyped());
        }
        return partnerMapper.selectByExample(partnerExample);
    }

    public RestResponse uploadExcel(MultipartFile file) {
        if (file == null)
            return RestResponse.build("1", "请上传文件");
        String postfix = FileUtil.getFilePostfix(file.getOriginalFilename());
        logger.info("文件后缀:" + postfix);
        if (!postfix.equals("xls")) {
            return RestResponse.build("1", "文件类型错误");
        }
        List<Map> list = null;
        try {
            InputStream is = file.getInputStream();
            if (is == null)
                System.out.println("输入流为空");
            list = jExcelService.readExcel(is);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Excel解析错");
        }
        if (list == null) {
            return RestResponse.build("1", "Excel解析错误");
        }
        return RestResponse.build(list);
    }

    public AvsPartner queryById(Long id) {
        return partnerMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void saveAll(String json) {
        Gson gson = new Gson();
        List<AvsPartner> list = gson.fromJson(json, new TypeToken<List<AvsPartner>>() {
        }.getType());
        for (int index = 0; index < list.size(); index++) {
            partnerMapper.insert(list.get(index));
        }
    }


    @Transactional
    public void deleteBatchByPartnerIds(Long[] partnerIds) {
        partnerExample.clear();
        partnerExample.createCriteria().andPartnerIdIn(ListUtils.getListByArrayLong(partnerIds));
        partnerMapper.deleteByExample(partnerExample);
    }

    public List<AvsPartner> queryByPartnerNameLike(Map<String, Object> params) {
        partnerExample.clear();
        if (params.containsKey("partnerName")) {
            PageHelper.startPage(1, 10);
            partnerExample.createCriteria().andPartnerNameLike("%" + params.get("partnerName").toString() + "%");
        }
        return partnerMapper.selectByExample(partnerExample);
    }

    public void save(AvsPartner partner) {
        partnerMapper.insert(partner);
    }

    public void update(AvsPartner partner) {
        partnerMapper.updateByPrimaryKey(partner);
    }
}
